<?php
/**
 * Created by PhpStorm.
 * Author: Wudean
 * Date: 2018/7/27 0027
 * Time: 17:19
 */

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Common extends Model
{
    /**
     * Ps: 2018-07-27 决定加一个基础模型 - Wudean
     */
    /**
     * @param null $obj
     * @return array|null
     * @name 模型数组集中处理
     * @user IHammer
     */
    public static function ModelArray($obj=null){
         if(!empty($obj)&&is_object($obj)){
            return $obj->toArray();
         }
         if(is_array($obj)){
            return $obj;
         }
        return [];
    }

    /**
     * @param $where
     * @param int $pageSize
     * @return array
     * @name  返回列表数据
     * @user IHammer
     */
    public  static function getListsUseDB($where=array(),$pageSize=10){
         $res = self::where($where)->paginate($pageSize);
         if($res){
            return $res->toArray();
         }
        return [];
    }

    /**
     * @param $where
     * @return array
     * @name 获取一条消息
     * @user IHammer
     */
    public static function getInfoUseDB($where=array()){
        $res = self::where($where)->first();
        if($res){
            $res->toArray();
        }
        return [];
    }

    /**
     * @param int $val
     * @param string $field
     * @return array
     * @name 根据指定id获取信息
     * @user IHammer
     */
    public static function getInfoByIdUseDB($val=0,$field='id'){
        $res = self::where($field,$val)->first();
        if($res){
            $res->toArray();
        }
        return [];
    }

    /**
     * @param array $createData
     * @return $this|bool|Model
     * @name 创建
     * @user IHammer
     */
    public static function createUseDB($createData=[]){
        if(!is_array($createData)||empty($createData)){
            return false;
        }
        $res = self::create($createData);
        if($res){
            return $res;
        }
        return false;
    }
}
